package epibot.BancoDeDados.ArmazenaDados;

public class Extract {

    /**
     * Extract a string of text from between the two specified tokens.  The 
     * case of the two tokens must match.  
     *
     * @param url The URL to download.
     * @param token1 The text, or tag, that comes before the desired text.
     * @param token2 The text, or tag, that comes after the desired text.
     * @param count Which occurrence of token1 to use, 1 for the first.
     * @return The contents of the URL that was downloaded.
     */
    public String extract(String str, String token1, String token2, int count) {
        int location1, location2;

        location1 = location2 = -1;
        do {
            // Start point in the string
            location1 = str.indexOf(token1, location1 + 1);
            if (location1 == -1) {
                return null;
            }
            count--;
        } while (count > 0);

        // End point in the string
        location2 = str.indexOf(token2, location1 + 1);
        if (location2 == -1) {
            return null;
        }

        return str.substring(location1 + token1.length(), location2);
    }

    /**
     * Extrai a informação da proteina
     * @param s
     * @return 
     */
    public String extractInformacao(String s) {
        String[] p = s.replace("\r\n", "#").replace("\n", "#").replace("\r", "#").split("#");
        return ">gi" + p[0];
    }

    /**
     * Extrai a sequencia da proteina
     * @param s
     * @return 
     */
    public String extractSequencia(String s) {
        String seq = "";

        String[] p = s.replace("\r\n", "#").replace("\n", "#").replace("\r", "#").split("#");

        for (int j = 1; j < p.length; j++) {
            seq += p[j]+"\n";
        }
        return seq;
    }
}
